package com.foreveross.proxyip.core.pools;

import java.util.List;

import com.dayatang.domain.InstanceFactory;
import com.foreveross.proxyip.core.BaseCode;
import com.foreveross.proxyip.core.pools.thread.CircleThread;
import com.foreveross.proxyip.core.pools.thread.db.DbCheckInThread;
import com.foreveross.proxyip.core.pools.thread.db.DbCheckOutThread;
import com.foreveross.proxyip.core.pools.validate.DbTestProxyIp;
import com.foreveross.proxyip.core.redis.PiRedisExecption;
import com.foreveross.proxyip.core.zk.task.doLoad.IDoLoad;
import com.foreveross.proxyip.entity.ProxyIpBean;
import com.foreveross.proxyip.exception.ProxyIpServiceException;
import com.foreveross.proxyip.infra.IpstoreRepository;
public class DbPool extends AvailablePool implements IDoLoad {
	public final static String POOL_CODE = "db";
	IpstoreRepository repository = InstanceFactory.getInstance(IpstoreRepository.class,"ipstoreRepositoryImpl");
	public Integer extractMaxNum = 0;
	public DbPool() throws NumberFormatException, PiRedisExecption {
		super("数据库池",POOL_CODE);
//		this.auth = channel.getAuth();
//		this.url = channel.getValidDefaultUrl();
		
		super.validate = new DbTestProxyIp(poolSign);
		initBaseCode();
//		checkOut();
//		checkIn();
	}
	
	@Override
	public void initChannelProperty() {
	}


	@Override
	public void initBaseCode() throws NumberFormatException, PiRedisExecption {
		// TODO Auto-generated method stub
		extractMaxNum = Integer.parseInt(BaseCode.getValue("DB_EXTRACT_NUM")); 
		super.sourceNum = Long.parseLong(BaseCode.getValue("DB_SOURCE_NUM"));
		super.proxyMax = Long.parseLong(BaseCode.getValue("DB_PROXY_MAX")); 
		super.threadMaxIpCount = Integer.parseInt(BaseCode.getValue("DB_THREAD_MAX_IP_COUNT")); 
		super.proxyMin = Long.parseLong(BaseCode.getValue("DB_PROXY_MIN")); 	
		super.checkInInterval = Long.parseLong(BaseCode.getValue("DB_IN_SEPARATION_TIME"));
		super.checkOutInterval = Long.parseLong(BaseCode.getValue("DB_OUT_SEPARATION_TIME"));
		this.validate.setConfig();
	}

	
	
	/**
	 * ip放进池里
	 * @param ip
	 * @param proxyIpBean
	 * @throws PiRedisExecption 
	 * @throws Exception 
	 */
	public void set(ProxyIpBean proxyIpBean) throws PiRedisExecption{
		if(!poolIpMap.containsKey(proxyIpBean.getCompletionIP())){//已经存在了
			proxyIpBean.getUsingKeys().add(this.poolSign);
			poolIpMap.set(proxyIpBean.getCompletionIP());
		}
	}

	@Override
	public void checkIn() {
		CircleThread run = null;
		Thread checkInTh = null;
		
		try {
			for (int i = 0; i < threadNum; i++) {
				run = new DbCheckInThread(this);
				checkInTh = new Thread(run, this.poolSign + "-checkin-" + i);
				this.checkInThs.add(checkInTh);
				checkInTh.start();
			}
		} catch (Exception e) {
			log.error(ProxyIpServiceException.getStackTrace(e));
			System.exit(0);
		}
	}

	@Override
	public void checkOut() {
		CircleThread run = null;
		Thread checkOutTh = null;
		// 检出线程比检入少一半
		int outThreadNum = threadNum / 2;
		outThreadNum = outThreadNum == 0 ? 1 : outThreadNum;

		try {
			for (int i = 0; i < outThreadNum; i++) {
				run = new DbCheckOutThread(this);
				checkOutTh = new Thread(run, this.poolSign + "-checkout-" + i);
				this.checkOutThs.add(checkOutTh);
				checkOutTh.start();
			}
		} catch (Exception e) {
			log.error(ProxyIpServiceException.getStackTrace(e));
		}

	}

	
//	public List getCheckInList(Long num) throws Exception {
//		
//	}

	protected List<ProxyIpBean> lastProcess(List<ProxyIpBean> returnList) throws Exception {
		for(ProxyIpBean proxyIp:returnList){
			lastProcessAction(proxyIp,PublicPool.POOL_CODE);
			
		}
		return returnList;
	}

	@Override
	public void lastProcessAction(ProxyIpBean bean, String poolCode)
			throws Exception {
			this.remove(bean);
	}


	@Override
	public List<ProxyIpBean> download(Long num) throws Exception {
		Integer numi = num>Integer.MAX_VALUE?Integer.MAX_VALUE:num.intValue();
		return repository.dbpoolIpList(numi);
	}



}
